package com.example.ISearchBlog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Value("${imgpath}")
    private String imagePath;
    /*
    * 映射外部图片路径
    * 存储的图片不管放到哪然后在前端访问的时候都是在下边映射的路径下去找图片
    * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").
                addResourceLocations("file:" + imagePath);
    }

    //只有重写了这个方法之后才能把拦截器注入到当前配置文件中
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") //拦截所有 url 地址
                .excludePathPatterns("/login.html")  //下面的是不拦截的 url
                .excludePathPatterns("/reg.html")
                .excludePathPatterns("/blog_list.html")
                .excludePathPatterns("/blog_content.html")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/editor.md/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/user/reg")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/art/addrcount")
                .excludePathPatterns("/user/islogin")
                .excludePathPatterns("blog_edit.html")
                .excludePathPatterns("/user/getcode") // 登录之前不能拦截验证码
                .excludePathPatterns("/art/getlistbypage")
                .excludePathPatterns("/art/getcount")
                .excludePathPatterns("/art/getdetail")
                .excludePathPatterns("art/search")
                .excludePathPatterns("/image/**");//登录和注册的接口也是不能拦截的
    }
}
